package pkg.drowne.org;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

public class Connection {

	private static final String NAMESPACE = "http://game.has/";
	private static final String URL = "http://valeriodidonato.itu.dk:8080/HasWebService/GameServicesService?xsd=1";
	private static final String SOAP_ACTION = "GameServicesService";

	public static boolean checkLogin(String username, String hashedPassword) {

		String METHOD_NAME = "Login";

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		request.addProperty("username", username);
		request.addProperty("password", hashedPassword);

		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			Object response = envelope.getResponse();
			if (response.toString().equals("1"))
				return true;
			else
				return false;

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		

	}

	public static boolean register(String username, String hashedPassword,
			String nickname, String emailAddress) {
		String METHOD_NAME = "Register";

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		request.addProperty("username", username);
		request.addProperty("password", hashedPassword);
		request.addProperty("nickname", nickname);
		request.addProperty("email", emailAddress);

		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			Object response = envelope.getResponse();
			Integer ret = new Integer(response.toString());
			if (ret == 1)
				return true;
			else
				return false;

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
	}

	public static int createGame(String owner, int duration) {
		String METHOD_NAME = "CreateNewGame";

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		request.addProperty("owner", owner);
		request.addProperty("duration", duration);

		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			Object response = envelope.getResponse();
			
			Log.d("GAME CREATED: ", response.toString());
			
			Integer ret = new Integer(response.toString());
			if (ret != -1)
				return ret;
			else
				return -1;

		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
		
		

	}

	public static String getAvailableGames() {

		String METHOD_NAME = "GetAvailableGames";
		Object result = null;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);

			result = envelope.getResponse();

		} catch (Exception e) {
			e.printStackTrace();
			return "EMPTY";
		}

		return result.toString();
	}

	public static int joinGame(String username, int idgame) {

		String METHOD_NAME = "JoinGame";
		Object result = null;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		request.addProperty("username", username);
		request.addProperty("idgame", idgame);

		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);

			result = envelope.getResponse();

		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}

		return Integer.parseInt(result.toString());
	}
	
	public static String getPlayersList(int idgame) {

		String METHOD_NAME = "GetPlayersList";
		Object result = null;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		request.addProperty("idgame", idgame);
		
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
		    
			androidHttpTransport.call(SOAP_ACTION, envelope);

			result = envelope.getResponse();
			Log.d("PLAYERSLIST", result.toString());
			return result.toString();

		} catch (Exception e) {
			e.printStackTrace();
			return "EMPTY";
		}

		
	}
	
	public static int setPosition(int gameSessionID, int latitude, int longitude) {

        String METHOD_NAME = "SetPosition";
        Object result = null;

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        
        request.addProperty("gameSessionID", gameSessionID);
        request.addProperty("latitude", latitude);
        request.addProperty("longitude", longitude);
        
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);

            result = envelope.getResponse();

        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }

        return Integer.parseInt( result.toString() );
    }
	
	public static int setCenterPosition(int idgame, int latitude, int longitude) {

        String METHOD_NAME = "SetCenterPosition";
        Object result = null;

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

        Log.d("idgame: ", idgame+"");
        Log.d("latitude: ", latitude+"");
        Log.d("longitude: ", longitude+"");
        
        request.addProperty("idgame", idgame);
        request.addProperty("centerlat", latitude);
        request.addProperty("centerlon", longitude);
        
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);

            result = envelope.getResponse();
            Log.d("POSITION SAVED: ", result.toString());
            
            return Integer.parseInt( result.toString() );

        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }

        
    }
	
   public static String getCenterPosition(int idgame) {

        String METHOD_NAME = "GetCenterPosition";
        Object result = null;

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

        request.addProperty("idgame", idgame);
        
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);

            result = envelope.getResponse();
            Log.d("CENTER COORDINATES", result.toString());
            
            return result.toString();

        } catch (Exception e) {
            e.printStackTrace();
            return "EMPTY";
        }

        
    }

    public static int getGamePhase(int idgame) {

        String METHOD_NAME = "GetGamePhase";
        Object result = null;

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

        request.addProperty("idgame", idgame);
        
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);

            result = envelope.getResponse();
            Log.d("GET GAMEPHASE", result.toString());
            
            return Integer.parseInt(result.toString());

        } catch (Exception e) {
            e.printStackTrace();
            
            return -1;
        }

        
    }
    
    public static int setGamePhase(int idgame, int gamePhase) {

        String METHOD_NAME = "SetGamePhase";
        Object result = null;

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

    
        request.addProperty("idgame", idgame);
        request.addProperty("gamePhase", gamePhase);
        
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);

            result = envelope.getResponse();
            
            Log.d("SET GAMEPHASE RESULT: ", result.toString());

            return Integer.parseInt( result.toString() );
            
        } catch (Exception e) {
            e.printStackTrace();
            
            return -1;
        }

        
    }
   
      public static String retrievePlayerPosition(String username, int idgame) {

            String METHOD_NAME = "RetrievePlayerPosition";
            Object result = null;

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            
            request.addProperty("username", username);
            request.addProperty("idgame", idgame);
            
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            try {
                androidHttpTransport.call(SOAP_ACTION, envelope);

                result = envelope.getResponse();
                Log.d("PLAYER POSITION: ", result.toString());
                
                return result.toString();

            } catch (Exception e) {
                e.printStackTrace();
                
                return "EMPTY";
            }

            
        }

      public static String retrieveAllPlayerPosition(int idgame) {

          String METHOD_NAME = "RetrieveAllPlayerPosition";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);
          
          request.addProperty("idgame", idgame);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("PLAYERS POSITIONS: ", result.toString());
              
              return result.toString();

          } catch (Exception e) {
              e.printStackTrace();
              
              return "EMPTY";
          }

          
      }
      
      public static String retrievePlayerPositionByGameSession(int gamesessionID) {

          String METHOD_NAME = "RetrievePlayerPositionByGameSession";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);
          
          request.addProperty("gamesessionID", gamesessionID);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("PLAYER LOCATION: ", result.toString());
              
              return result.toString();

          } catch (Exception e) {
              e.printStackTrace();
              
              return "EMPTY";
          }

          
      }
      
      public static int getNumPlayers(int idgame) {

          String METHOD_NAME = "GetNumPlayers";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);

          request.addProperty("idgame", idgame);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("NUM PLAYERS: ", result.toString());
              
              return Integer.parseInt(result.toString());

          } catch (Exception e) {
              e.printStackTrace();
              
              return -1;
          }
  
      }
      
      public static int assignTeam(int gamesessionID, int teamID) {

          String METHOD_NAME = "AssignTeam";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);

          request.addProperty("gamesessionID", gamesessionID);
          request.addProperty("teamID", teamID);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("SET TEAM "+teamID+" RESULT: ", result.toString());
              
              return Integer.parseInt(result.toString());

          } catch (Exception e) {
              e.printStackTrace();
              
              return -1;
          }
 
      }
      
      public static int autoAssignTeams(int idgame) {

          String METHOD_NAME = "AutoAssignTeams";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);

          request.addProperty("idgame", idgame);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("NUM SEEKERS: ", result.toString());
              
              return Integer.parseInt(result.toString());

          } catch (Exception e) {
              e.printStackTrace();
              
              return -1;
          }

      }
      
      public static int getTeam(int gamesessionID) {

          String METHOD_NAME = "GetTeam";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);

          request.addProperty("gamesessionID", gamesessionID);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("GET TEAM: ", result.toString());
              
              return Integer.parseInt(result.toString());

          } catch (Exception e) {
              e.printStackTrace();
              
              return -1;
          }

          
      }
      
      public static int getPlayerState(int gamesessionID) {

          String METHOD_NAME = "GetPlayerState";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);

          request.addProperty("gamesessionID", gamesessionID);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("GET PLAYERSTATE: ", result.toString());
              
              return Integer.parseInt(result.toString());

          } catch (Exception e) {
              e.printStackTrace();
              return -1;
          }

          
      }
      
      public static int getSimplePlayerState(int gamesessionID) {

          String METHOD_NAME = "GetSimplePlayerState";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);

          request.addProperty("gamesessionID", gamesessionID);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("GET SIMPLEPLAYERSTATE: ", result.toString());
              
              return Integer.parseInt(result.toString());

          } catch (Exception e) {
              e.printStackTrace();
              
              return -1;
          }

          
      }
      
      public static int setPlayerState(int gamesessionID, int state) {

          String METHOD_NAME = "SetPlayerState";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);

          request.addProperty("gamesessionID", gamesessionID);
          request.addProperty("state", state);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("SET PLAYER STATE: ", result.toString());
              return Integer.parseInt(result.toString());

          } catch (Exception e) {
              e.printStackTrace();
              return -1;
          }

          
      }
 
      public static String retrieveTeamMatesPositions(int idgame, int gamesessionID, int team) {

          String METHOD_NAME = "RetrieveTeamMatesPositions";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);
          
          request.addProperty("idgame", idgame);
          request.addProperty("gamesessionID", gamesessionID);
          request.addProperty("team", team);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("TEAMMATES POSITIONS: ", result.toString());
              
              return result.toString();

          } catch (Exception e) {
              e.printStackTrace();
              
              return "EMPTY";
          }

          
      }
      
      public static double calculateDistance(String latVal1, String lonVal1, String latVal2, String lonVal2) {

          String METHOD_NAME = "CalculateDistance";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);
          
          request.addProperty("latVal1", latVal1);
          request.addProperty("lonVal1", lonVal1);
          request.addProperty("latVal2", latVal2);
          request.addProperty("lonVal2", lonVal2);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("DISTANCE: ", result.toString());
              
              Double doubleResult = Double.parseDouble(result.toString());
              return doubleResult.doubleValue();

          } catch (Exception e) {
              e.printStackTrace();
              return -1;
          }


      }

      public static int getCaptureState(int gamesessionID) {

          String METHOD_NAME = "GetCaptureState";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);

          request.addProperty("gamesessionID", gamesessionID);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("GET CAPTURESTATE: ", result.toString());
              return Integer.parseInt(result.toString());

          } catch (Exception e) {
              e.printStackTrace();
              return -1;
          }

          
      }
      
      public static int setCaptureState(int gamesessionID, int state) {

          String METHOD_NAME = "SetCaptureState";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);

          request.addProperty("gamesessionID", gamesessionID);
          request.addProperty("state", state);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("SET CAPTURESTATE: ", result.toString());

              return Integer.parseInt(result.toString());
              
          } catch (Exception e) {
              e.printStackTrace();
              return -1;
          }

          
      }
      
      public static int getClosestEnemy(int idgame, int gamesessionID, int team) {

          String METHOD_NAME = "GetClosestEnemy";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);
          
          request.addProperty("gamesessionID", gamesessionID);
          request.addProperty("idgame", idgame);
          request.addProperty("team", team);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("TEAMMATES POSITIONS: ", result.toString());
              return Integer.parseInt(result.toString());

          } catch (Exception e) {
              e.printStackTrace();
              return -1;
          }

      }
      
      public static int getRemainingTime(int idgame) {

          String METHOD_NAME = "GetRemainingTime";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);
          
          request.addProperty("idgame", idgame);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("REMAINING TIME: ", result.toString());
              return Integer.parseInt( result.toString() );

          } catch (Exception e) {
              e.printStackTrace();
              return -1;
          }
 
      }
      
      public static int getNumberOfEnemies(int idgame, int team) {

          String METHOD_NAME = "GetNumberOfEnemies";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);
          
          request.addProperty("idgame", idgame);
          request.addProperty("team", team);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("NUMBER OF ENEMIES: ", result.toString());
              
              return Integer.parseInt(result.toString());

          } catch (Exception e) {
              e.printStackTrace();
              
              return -1;
          }

          
      }
      
      public static int checkValidLocations(int idgame) {

          String METHOD_NAME = "CheckValidLocations";
          Object result = null;

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                  SoapEnvelope.VER11);
          
          request.addProperty("idgame", idgame);
          
          envelope.setOutputSoapObject(request);
          HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

          try {
              androidHttpTransport.call(SOAP_ACTION, envelope);

              result = envelope.getResponse();
              Log.d("ARE LOCATIONS VALID: ", result.toString());
              
              return Integer.parseInt(result.toString());

          } catch (Exception e) {
              e.printStackTrace();
              return -1;
          }

          
      }
      
}
